// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package resource_dao

import (
	"context"
	"regexp"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/resource_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type BasicResourceInfoDbItem struct {
	Name string `bson:"name"`
	Desc string `bson:"desc"`
}

type ResourceInfoDbItem struct {
	ResourceId   string                     `bson:"_id"`
	ResourceType resource_api.RESOURCE_TYPE `bson:"resourceType"`
	BasicInfo    BasicResourceInfoDbItem    `bson:"basicInfo"`
	OwnerUserId  string                     `bson:"ownerUserId"`
	CreateTime   int64                      `bson:"createTime"`
	UpdateTime   int64                      `bson:"updateTime"`
}

func (item *ResourceInfoDbItem) ToResourceInfo() *resource_api.ResourceInfo {
	return &resource_api.ResourceInfo{
		ResourceId:   item.ResourceId,
		ResourceType: item.ResourceType,
		BasicInfo: &resource_api.BasicResourceInfo{
			Name: item.BasicInfo.Name,
			Desc: item.BasicInfo.Desc,
		},
		OwnerUserId: item.OwnerUserId,
		CreateTime:  item.CreateTime,
		UpdateTime:  item.UpdateTime,
	}
}

type _ResourceInfoDao struct {
	mc *mongo.Collection
}

func newResourceInfoDao(db *mongo.Database) (*_ResourceInfoDao, error) {
	mc := db.Collection(common.DB_NS_RESOURCE + "info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"ownerUserId", 1}, {"createTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_ResourceInfoDao{mc: mc}, nil
}

func (db *_ResourceInfoDao) Insert(ctx context.Context, dbItem *ResourceInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_ResourceInfoDao) Exist(ctx context.Context, resourceId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": resourceId})
	return count > 0, err
}

func (db *_ResourceInfoDao) Get(ctx context.Context, resourceId string) (*ResourceInfoDbItem, error) {
	retItem := &ResourceInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": resourceId}).Decode(retItem)
	return retItem, err
}

func (db *_ResourceInfoDao) Remove(ctx context.Context, resourceId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": resourceId})
	return err
}

func (db *_ResourceInfoDao) UpdateBasic(ctx context.Context, resourceId string, basicItem *BasicResourceInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": resourceId}, bson.M{
		"$set": bson.M{
			"basicInfo":  basicItem,
			"updateTime": updateTime,
		}})
	return err
}

func (db *_ResourceInfoDao) CountByOwner(ctx context.Context, ownerUserId string, filterByResourceType bool, resourceType resource_api.RESOURCE_TYPE, filterByKeyword bool, keyword string) (uint32, error) {
	query := bson.M{
		"ownerUserId": ownerUserId,
	}
	if filterByResourceType {
		query["resourceType"] = resourceType
	}
	if filterByKeyword {
		query["basicInfo.name"] = bson.Regex{
			Pattern: regexp.QuoteMeta(keyword),
			Options: "i",
		}
	}

	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_ResourceInfoDao) ListByOwner(ctx context.Context, ownerUserId string, filterByResourceType bool, resourceType resource_api.RESOURCE_TYPE, filterByKeyword bool, keyword string, offset, limit int64) ([]*ResourceInfoDbItem, error) {
	query := bson.M{
		"ownerUserId": ownerUserId,
	}
	if filterByResourceType {
		query["resourceType"] = resourceType
	}

	if filterByKeyword {
		query["basicInfo.name"] = bson.Regex{
			Pattern: regexp.QuoteMeta(keyword),
			Options: "i",
		}
	}

	cursor, err := db.mc.Find(ctx, query, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*ResourceInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
